<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <lb-table
      :column="tableData.column"
      :data="tableData.data"
      pagination
      background
      border
      height="690"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 40, 60, 80]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
    >
    </lb-table>
    <operate :visible.sync="show" @close="close"></operate>
    <approval :id="id" :visible.sync="show3" @close="hanleClose"></approval>
  </lb-layout>
</template>

<script>
import { list, deleteUp } from "@/api/basicdata/uploadManage";
import operate from "@/views/configuration/basicdata/uploadManage/components/upload";
import approval from "@/views/configuration/basicdata/uploadManage/components/approval";
import auth from "@/plugins/auth";

export default {
  name: "uploadManage",
  dicts: ["sub_application", "show_plate", "document_type", "upload_status"],
  components: {
    operate,
    approval,
  },
  data() {
    return {
      id: "",
      show3: false,
      //弹窗
      show: false,
      apsForm: {
        formRef: "form",
        labelWidth: "100px",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            //这里是渲染查询表单的表头和类型的数据
            documentTitle: {
              label: "文件标题:",
              typeName: "el-input",
              clearable: true,
              placeholder: "请输入文件标题",
            },
            documentType: {
              label: "文献种类:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [];
                that.dict?.type?.document_type.some((item) => {
                  arr.push(item);
                });
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().documentType;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择文献种类",
                        value: valNum,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue(
                            "documentType",
                            value
                          );
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: parseInt(value),
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            subApplication: {
              label: "子应用:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [];
                that.dict?.type?.sub_application.some((item) => {
                  arr.push(item);
                });
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().subApplication;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择子应用",
                        value: valNum,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue(
                            "subApplication",
                            value
                          );
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: parseInt(value),
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            showPlate: {
              label: "显示板块:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [];
                that.dict?.type?.show_plate.some((item) => {
                  arr.push(item);
                });
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().showPlate;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择显示板块",
                        value: valNum,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("showPlate", value);
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: parseInt(value),
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            // completionTime: {
            //   typeName: "el-date-picker",
            //   type: "date",
            //   "value-format": "yyyy-MM-dd",
            //   placeholder: "请选择上传时间",
            // },
            startAndend: {
              label: "上传时间:",
              typeName: "el-date-picker",
              type: "datetimerange",
              size: "small",
              "start-placeholder": "开始时间",
              "end-placeholder": "结束时间",
              "value-format": "yyyy-MM-dd HH:mm:ss",
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
            {
              label: "新增",
              type: "primary",
              typeName: "el-button",
              plain: true,
              size: "small",
              icon: "el-icon-plus",
              display: true,
              click: () => this.showModel(),
            },
          ],
        },
      },
      tableData: {
        column: [
          {
            prop: "documentTitle",
            label: "文件标题",
            showOverflowTooltip: true,
            minWidth: 120,
          },
          {
            prop: "documentType",
            label: "文献种类",
            minWidth: 120,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.document_type}
                    value={scope.row.documentType}
                  />
                </div>
              );
            },
          },
          {
            prop: "subApplication",
            label: "子应用",
            minWidth: 120,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.sub_application}
                    value={scope.row.subApplication}
                  />
                </div>
              );
            },
          },
          {
            prop: "showPlate",
            label: "显示板块",
            minWidth: 200,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.show_plate}
                    value={scope.row.showPlate}
                  />
                </div>
              );
            },
          },
          {
            prop: "fileDesc",
            label: "文件内容描述",
            showOverflowTooltip: true,
          },
          {
            prop: "createTime",
            label: "上传时间",
            width: 180,
          },
          {
            prop: "createUserName",
            label: "上传人",
            width: 100,
          },
          {
            prop: "status",
            label: "状态",
            width: 150,
            align: "center",
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.upload_status}
                    value={scope.row.uploadStatus}
                  />
                </div>
              );
            },
          },
          {
            label: "操作",
            width: "240",
            fixed: "right",
            render: (h, scope) => {
              let that = this;
              let a;
              let b;
              if (scope.row.uploadStatus == 0 || scope.row.uploadStatus == 1) {
                a = (
                  <el-button
                    v-hasPermi={[
                      "system:management:audit",
                      "system:management:upload",
                      "system:management:reject",
                    ]}
                    type="text"
                    size="small"
                    icon="el-icon-edit-outline"
                    onClick={() => {
                      that.handleApproval(scope.row);
                    }}
                  >
                    审核
                  </el-button>
                );
              } else {
                a = <div style="display:none"></div>;
              }
              if (scope.row.uploadStatus == 2) {
                b = (
                  <el-button
                    v-hasPermi={["system:management:upload"]}
                    type="text"
                    icon="el-icon-upload2"
                    size="small"
                    onClick={() => {
                      that.handleApproval(scope.row);
                    }}
                  >
                    上传
                  </el-button>
                );
              } else {
                b = <div style="display:none"></div>;
              }
              return (
                <div>
                  {a}
                  {b}
                  {
                    <el-button
                      style={"margin-left:10px"}
                      v-hasPermi={["system:management:query"]}
                      icon={"el-icon-download"}
                      type="text"
                      size="small"
                      onClick={() => {
                        this.preview(scope.row.filePath);
                      }}
                    >
                      下载预览
                    </el-button>
                  }
                  {
                    <el-button
                      v-hasPermi={["system:management:remove"]}
                      type="text"
                      size="small"
                      icon="el-icon-delete"
                      onClick={() => {
                        this.handleOperDelete(scope.row);
                      }}
                      style="color:red"
                    >
                      删除
                    </el-button>
                  }
                </div>
              );
            },
          },
        ],
        data: [],
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
    };
  },
  mounted() {
    // 添加总会管理员角色判断zjuadmin
    this.judgePermi();
    this.initData();
  },
  methods: {
    judgePermi() {
      if (auth.hasPermi("system:management:add")) {
        this.apsForm.formConfig.formSoltButton[2].display = false;
      }
    },
    preview(url) {
      if (!url) {
        this.$message.warning("缺少文件");
        return false;
      }
      window.open(process.env.VUE_APP_BASE_API + "/" + url);
    },
    handleApproval(row) {
      this.id = row.id || "";
      this.show3 = true;
    },
    hanleClose(status) {
      this.show3 = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
    // 弹窗
    showModel() {
      this.show = true;
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
    //删除
    handleOperDelete(row) {
      let that = this;
      const text = "是否确认删除" + (row?.name ?? "该条数据") + "？";
      this.$modal
        .confirm(text)
        .then(() => {
          that.$store.commit("app/updateApiCount", "add");
          deleteUp(row.id)
            .then((res) => {
              that.$store.commit("app/updateApiCount");
              if (res.code === 700) {
                that.$message.warning(res.msg);
                return false;
              }

              that.$message.success("已删除!");
              that.initData();
            })
            .catch((err) => {
              that.$store.commit("app/updateApiCount");
              // this.$message.error("系统错误,请联系管理员!300802");
            });
        })
        .catch(() => {});
    },
    async initData(currentPage, pageSize, data) {
      let that = this;
      let query = {
        ...data,
      };
      if (currentPage) {
        query.pageNum = currentPage;
      } else {
        query.pageNum = that.currentPage;
      }
      if (pageSize) {
        query.pageSize = pageSize;
      } else {
        query.pageSize = that.pageSize;
      }
      that.$store.commit("app/updateApiCount", "add");
      await list(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            that.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res.rows;
          that.total = res.total;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!300801");
        });
    },
    handlerSearchDetail(val) {
      //val===1 删除操作成功后分页边界值处理
      let currentPage = 1;
      if (val === 1) {
        if (
          this.total === (this.currentPage - 1) * this.pageSize + 1 &&
          this.total !== 0 &&
          this.currentPage > 1
        ) {
          currentPage = this.currentPage - 1;
          this.currentPage = currentPage;
        } else {
          currentPage = this.currentPage;
        }
      } else {
        currentPage = this.currentPage;
      }
      const { startAndend, ...other } = this.$refs?.form?.getFieldsValue();
      const data = {
        ...other,
      };
      if (startAndend) {
        data.startTime = startAndend[0];
        data.endTime = startAndend[1];
      }
      this.initData(currentPage, this.pageSize, data);
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    handleReset() {
      //重置
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
  },
};
</script>
